package com.yty.newconsult.handler;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yty.newconsult.common.ChannelManager;
import com.yty.newconsult.service.InvokeApiService;
import com.yty.newconsult.utils.SpringContextUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;

public class TaskHandler {

    private InvokeApiService invokeApiService;
    private ThreadPoolExecutor pool;
    public TaskHandler()
    {
        invokeApiService= SpringContextUtil.getBean(InvokeApiService.class);
        pool=new ThreadPoolExecutor(6,6,60,java.util.concurrent.TimeUnit.SECONDS,new LinkedBlockingQueue<Runnable>());
    }
    public void submit(ChannelHandlerContext ctx, String text) {
        JSONObject obj= JSONUtil.parseObj(text);
        pool.execute(new Runnable() {
            @Override
            public void run() {
                switch (obj.getStr("type")){
                    case "chat":
                        invokeApiService.queryDsSdk(obj.getStr("question"), (String)obj.get("id"),ctx);
                        break;
                    case "summary":
                        invokeApiService.querySummarySdk((String)obj.get("id"),obj.getStr("question"), ctx);
                        break;
                    case "login":
                        System.out.println("创建通道");
                        ChannelManager.addChannel((String)obj.get("id"),ctx.channel());
                        break;
                 }
            }
        });

    }

}
